﻿#include "FontIcon.h"

#include <QFontDatabase>
#include <QDebug>

FontIcon::FontIcon(QObject *parent) : QObject(parent)
{
    _fontID = -1;
}

bool FontIcon::importFontIcon(const QString &path)
{
    _fontID = QFontDatabase::addApplicationFont(path); // 加入字体，并获取字体ID
    if (_fontID == -1)
        return false;

    _fontNameList = QFontDatabase::applicationFontFamilies(_fontID);
    return true;
}

QFont FontIcon::getFont(int pixelSize)
{
    QFont font;
    if (_fontNameList.contains("FontAwesome"))
    {
        font = QFont("FontAwesome");
        font.setHintingPreference(QFont::PreferNoHinting);
    }
    font.setPixelSize(pixelSize);

    return font;
}

QChar FontIcon::getIconText(int index)
{
    return QChar(0xf000 + index);
}

QString FontIcon::getIconTip(int index)
{
    return QString("0xf%1 (%2)").arg(index, 3, 16, QChar('0')).arg(index);;
}
